<template>
  <div class="header" :style="{top:isNavVisible?'0px':'-100%'} ">
    <div
      v-show="!name"
      class="left"
    >
      <div class="image">
        <img
          :src="oldSrc"
          alt=""
        >
      </div>
      <div class="txt">
        <router-link to="/login">去登录</router-link>
      </div>
    </div>
    <div
      v-show="name"
      class="left"
    >
      <div class="image">
        <img
          :src="newSrc ? 'http://localhost:3000'+newSrc :oldSrc"
          alt=""
        >
      </div>
      <div class="txt">
        <h2>{{name}}</h2><span>用户您好</span>
        <a @click="exit">退出登录</a>
      </div>
    </div>
    <ul class="center">
      <li v-for="(item, index) in list" :key="index">
        <router-link  active-class="active"  :to="`/index/${item.url}`">{{item.name}}</router-link>
      </li>
    </ul>
    <div class="right">
      <img
        src="../assets/images/logo.svg"
        alt=""
      >
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "MyHeader",
  data() {
    return {
      isNavVisible :true,
      actived:0,
      list: [
        { name: "关于海底捞", url: "home" },
        { name: "菜品", url: "foods" },
        { name: "订单", url: "order" },
        { name: "个人中心", url: "user" },
      ],
      name: localStorage.getItem("name") || "",
      oldSrc:
        "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
      newSrc: "",
    };
  },
  mounted() {
    this.newSrc = this.info.avatar || "";
     window.addEventListener('scroll', this.handleScroll);
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll);
},
  computed: {
    ...mapState({
      info: (state) => {
        return state.user.info;
      },
    }),
  },
  methods: {
    exit() {
      this.$store.commit('DEL_ALL_NEWLIST')
      localStorage.clear();
      this.$router.push("/login");
    },
    toOther(index,url){
      this.actived = index
      this.$router.push(`/index${url}`)
    },
    handleScroll() {
    const scrollTop = window.scrollY;
    if (scrollTop > 400) { // 可以根据需要调整这个值
      this.isNavVisible = false;
    } else {
      this.isNavVisible = true;
    }
  }
  },
  watch: {
    info() {
      this.newSrc = this.info.avatar || "";
    },
  },
};
</script>

<style lang="less">
.header {
  top: 0;
  position: fixed;
  z-index: 1;
  color: #888;
  display: flex;
  padding: 0 20px;
  justify-content: space-between;
  width: 100%;
  height: 100px;
  line-height: 100px;
  box-sizing: border-box;
  background-color: #fff;
  // border-bottom: 1px solid #888;
  transition: top 1.5s ease-in-out; /* 设置过渡效果 */
  .left {
    .image {
      margin-right: 20px;
      border: 50%;
      overflow: hidden;
      // background-color: blue;
      img {
        width: 88px;
        height: 88px;
        border-radius: 50%;
        animation: spin 4s linear infinite;
      }
    }
    @keyframes spin {
      0% {
        transform: rotate(0deg);
      }
      100% {
        transform: rotate(360deg);
      }
    }
    .txt {
      display: flex;
      h2 {
        margin-right: 10px;
      }
      span {
        margin-right: 10px;
      }
      a {
        font-size: 18px;
        cursor: pointer;
      }
    }
    display: flex;
  }
  ul {
    margin-left: -300px;
    display: flex;
    li {
      margin-left: 80px;
      cursor: pointer;
      a {
        font-weight: 700;
        font-size: 18px;
      }
    }
    .active {
      color: red;
    }
  }
}
</style>